//
//  SimAlertView.m
//

#import "SimAlertView.h"
#import "SimLineView.h"
#import "SimPopKit.h"
#import "UIView+SimAdditions.h"

SimImpValue(kSimAlertViewTitleFont, UIFont *)
SimImpValue(kSimAlertViewMsgFont, UIFont *)
SimImpValue(kSimAlertViewBtnFont, UIFont *)

SimImpValue(kSimAlertViewTitleColor, UIColor *)
SimImpValue(kSimAlertViewMsgColor, UIColor *)
SimImpValue(kSimAlertViewBtnNormalColor, UIColor *)
SimImpValue(kSimAlertViewBtnConfirmColor, UIColor *)


@interface SimAlertView ()
{
    UILabel *_titleLabel;
    UILabel *_messageLabel;
    DismissAlertBlock _dismissBlock;
    ControlDismissAlertBlock _controlDismissBlock;
}

@property (nonatomic, strong) SimPopKit *popKit;

@end

@implementation SimAlertView



- (id)initWithTitle:(NSString *)title message:(NSString *)message btns:(NSArray *)btnsArray controlDismissBlock:(ControlDismissAlertBlock)block
{
    self = [self initWithTitle:title message:message btns:btnsArray];
    if (self) {
        _controlDismissBlock = block;
    }
    
    return self;
}

- (id)initWithTitle:(NSString *)title message:(NSString *)message btns:(NSArray *)btnsArray dismissBlock:(DismissAlertBlock)block
{
    self = [self initWithTitle:title message:message btns:btnsArray];
    if (self) {
        _dismissBlock = block;
    }
    
    return self;
}


- (id)initWithTitle:(NSString *)title message:(NSString *)message btns:(NSArray *)btnsArray{
    CGFloat vGap = (title.length > 0 && message.length > 0) ? 10 : 20;
    CGFloat top = vGap;
    CGFloat btnHeight = 44;
    CGFloat edge = 10;
    CGFloat alertWidth = DEVICE_IS_IPAD ? 320 : 260;

    if (self = [super initWithFrame:CGRectMake(0, 0, alertWidth, 0)]) {
        self.backgroundColor = HEXRGBCOLOR(0xffffff);
        self.layer.cornerRadius = 8;
        self.layer.masksToBounds = YES;
        
        if (title.length > 0) {
            UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(edge, top, alertWidth-2*edge, 0)];
            titleLabel.textAlignment = NSTextAlignmentCenter;
            titleLabel.backgroundColor = [UIColor clearColor];
            titleLabel.textColor = SimValue(kSimAlertViewTitleColor,  HEXRGBCOLOR(0x3f3f3f));
            titleLabel.font = SimValue(kSimAlertViewTitleFont, [UIFont systemFontOfSize:15.0]);
            titleLabel.numberOfLines = 0;
            titleLabel.text = title;
            CGSize titleLbSize = SIM_MULTILINE_TEXTSIZE2(titleLabel.text, titleLabel.font, CGSizeMake(titleLabel.width, CGFLOAT_MAX));
            titleLabel.height = titleLbSize.height;
            [self addSubview:titleLabel];
            top = titleLabel.bottom + vGap;
            _titleLabel = titleLabel;
        }
        
        if (message.length > 0) {
            UILabel *messageLabel = [[UILabel alloc] initWithFrame:CGRectMake(edge, top, alertWidth-2*edge, 0)];
            messageLabel.textAlignment = NSTextAlignmentCenter;
            messageLabel.backgroundColor = [UIColor clearColor];
            messageLabel.textColor = SimValue(kSimAlertViewMsgColor, HEXRGBCOLOR(0x555555));
            messageLabel.font = SimValue(kSimAlertViewMsgFont, [UIFont systemFontOfSize:13.0]);
            messageLabel.lineBreakMode = NSLineBreakByWordWrapping;
            messageLabel.numberOfLines = 0;
            messageLabel.text = message;
            CGSize msgLbSize = SIM_MULTILINE_TEXTSIZE2(messageLabel.text, messageLabel.font, CGSizeMake(messageLabel.width, CGFLOAT_MAX));
            messageLabel.height = msgLbSize.height;
            [self addSubview:messageLabel];
            top = messageLabel.bottom + vGap;
            _messageLabel = messageLabel;
        }

        SimLineView *bottomline = [[SimLineView alloc] initWithWidth:alertWidth];
        bottomline.top = top;
        [self addSubview:bottomline];
        
        float buttonWidth = alertWidth / btnsArray.count;
        for(int i = 0; i < btnsArray.count; i++){
            NSString *buttonTitle = [btnsArray objectAtIndex:i];
            UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
            [button setTitle:buttonTitle forState:UIControlStateNormal];
            button.titleLabel.font = SimValue(kSimAlertViewBtnFont, [UIFont boldSystemFontOfSize:15.0]);
            button.titleEdgeInsets = UIEdgeInsetsMake(-3, 0, 0, 0);
            button.backgroundColor = [UIColor clearColor];
            button.tag = i;
            button.frame = CGRectMake(buttonWidth *i, bottomline.origin.y +2, buttonWidth, btnHeight);
            [button addTarget:self action:@selector(didClickOnBtn:) forControlEvents:UIControlEventTouchUpInside];
            if (i != btnsArray.count - 1) {
                [button setTitleColor:SimValue(kSimAlertViewBtnNormalColor, HEXRGBCOLOR(0x858585))
                             forState:UIControlStateNormal];
                UIImageView *partImageView = [[UIImageView alloc]initWithFrame:CGRectMake(button.right-1, bottomline.origin.y + 2, 1, 40)];
                UIImage *partImage  = [UIImage imageNamed:@"part_line"];
                partImageView.image = partImage;
                [self addSubview:partImageView];
            }
            else{
                [button setTitleColor:SimValue(kSimAlertViewBtnConfirmColor, HEXRGBCOLOR(0x46b789))
                             forState:UIControlStateNormal];
                top = button.bottom;
            }
            [self addSubview:button];
        }
        self.height = top;

    }
    return self;
}

- (void)dealloc
{
    NSLog(@"dealloc");
}

- (void)didClickOnBtn:(UIButton *)btn
{
    BOOL shouldDismiss = YES;
    if (_controlDismissBlock) {
        shouldDismiss = _controlDismissBlock(btn.tag);
    }
    else if (_dismissBlock) {
        _dismissBlock(btn.tag);
    }
    
    if (shouldDismiss) {
        [self dismissAlertView];
    }
}

- (void)show
{
    if (_popKit) {
        return;
    }
    
    _popKit = [[SimPopKit alloc] init];
    _popKit.bTapDismiss = NO;
    [_popKit popView:self animateType:PAT_Scale];
    DefineWeak(self);
    _popKit.dismissBlock = ^{
        Weak(self).popKit = nil;
    };
}

- (void)dismissAlertView
{
    if (_popKit) {
        [_popKit dismiss:YES];
        _popKit = nil;
    }
}

- (void)setMsgTextAlignment:(NSTextAlignment)alignment
{
    _messageLabel.textAlignment = alignment;
}


@end
